package com.csii.mobilebanking.expandablelistview;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

/***
 * 
 * @author Hua
 *
 */
public class ExpandableAdapter extends BaseExpandableListAdapter {

	private String[] groups;
	private String[][] children;

	private Context mContext;

	public ExpandableAdapter(Context context, String[] groups,
			String[][] children) {
		this.mContext = context;
		this.groups = groups;
		this.children = children;
	}

	@Override
	public int getGroupCount() {

		return groups.length;
	}

	@Override
	public int getChildrenCount(int groupPosition) {

		return children[groupPosition].length;
	}

	@Override
	public Object getGroup(int groupPosition) {

		return groups[groupPosition];
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {

		return children[groupPosition][childPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {

		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {

		return childPosition;
	}

	@Override
	public boolean hasStableIds() {

		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		TextView textView = getGenericView();
		textView.setText(getGroup(groupPosition).toString());
		
		return textView;
	}

	public TextView getGenericView() {

		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT, 64);

		TextView textView = new TextView(mContext);
		textView.setLayoutParams(lp);
		// 涓績鍨傜洿鐨勬枃鏈�
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		// 璁剧疆鏂囧瓧鐨勯鍙戜綅缃�
		textView.setPadding(36, 0, 0, 0);
		return textView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		TextView textView = getGenericView();
		textView.setText(getChild(groupPosition, childPosition).toString());

		return textView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {

		return true;
	}

}
